﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Enterprise.DataFactory;
using Enterprise.Infrastructure;
using Enterprise.Model.Doc;
namespace Enterprise.Data.Doc
{
    /// <summary>
    /// 文档数据操作类
    /// </summary>
    public class DocData
    {
        /// <summary>
        /// 通知公告内容显示
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public DocArticleEntity DocArticleDisp(int articleId)
        {
            string cmd = "Select * from DOC_ARTICLES where ID=" + articleId;
            IDataReader dr = DbExecute.DataReader(cmd);
            return FormatArticleList(dr).FirstOrDefault();
        }

        /// <summary>
        ///  通知公告数据列表
        /// </summary>
        /// <returns></returns>
        public List<DocArticleEntity> DocArticleList(int moduleId)
        {
            string cmd = "Select * from DOC_ARTICLES where CLASSID=" + moduleId+" order by PUBDATE desc";
            IDataReader dr = DbExecute.DataReader(cmd);
            return FormatArticleList(dr);
        }

        /// <summary>
        ///  通知公告数据列表
        /// </summary>
        /// <returns></returns>
        public List<DocArticleEntity> DocArticleList(string cmd)
        {
            List<DocArticleEntity> list = new List<DocArticleEntity>();
            IDataReader dr = DbExecute.DataReader(cmd);
            return FormatArticleList(dr);
        }

        /// <summary>
        /// 通知公告数据操作方法
        /// </summary>
        /// <param name="cmd"></param>
        public void ArticleExecute(string cmd)
        {
            DbExecute.Execute(cmd);
        }

        /// <summary>
        /// 数据操作方法
        /// </summary>
        /// <param name="cmd"></param>
        public void ArticleExecute(string cmd, string item1, object item1Value, string item2, object item2Value)
        {
            DbExecute.ExecuteCommand(cmd, item1, item1Value, item2, item2Value);
        }

        /// <summary>
        /// 提取通用转换数据代码
        /// 格式化通知公告
        /// </summary>
        /// <param name="dr">数据集</param>
        /// <returns></returns>
        private List<DocArticleEntity> FormatArticleList(IDataReader dr)
        {
            List<DocArticleEntity> list = new List<DocArticleEntity>();
            while (dr.Read())
            {
                DocArticleEntity entity = new DocArticleEntity();
                if (!Convert.IsDBNull(dr["ID"])) entity.ID = Convert.ToInt32(dr["ID"]);
                if (!Convert.IsDBNull(dr["CLASSID"])) entity.CLASSID = Convert.ToInt32(dr["CLASSID"]);
                if (!Convert.IsDBNull(dr["TITLE"])) entity.TITLE = Convert.ToString(dr["TITLE"]);
                if (!Convert.IsDBNull(dr["CONTENTS"])) entity.CONTENTS = Convert.ToString(dr["CONTENTS"]);
                if (!Convert.IsDBNull(dr["TITLERU"])) entity.TITLERU = Convert.ToString(dr["TITLERU"]);
                if (!Convert.IsDBNull(dr["CONTENTSRU"])) entity.CONTENTSRU = Convert.ToString(dr["CONTENTSRU"]);
                if (!Convert.IsDBNull(dr["AUTHOR"])) entity.AUTHOR = Convert.ToString(dr["AUTHOR"]);
                if (!Convert.IsDBNull(dr["PUBDATE"])) entity.PUBDATE = Convert.ToDateTime(dr["PUBDATE"]);
                if (!Convert.IsDBNull(dr["ARTICLEFROM"])) entity.ARTICLEFROM = Convert.ToString(dr["ARTICLEFROM"]);
                if (!Convert.IsDBNull(dr["HITS"])) entity.HITS = Convert.ToInt32(dr["HITS"]);
                if (!Convert.IsDBNull(dr["KEYS"])) entity.KEYS = Convert.ToString(dr["KEYS"]);
                if (!Convert.IsDBNull(dr["ATOP"])) entity.ATOP = Convert.ToInt32(dr["ATOP"]);
                if (!Convert.IsDBNull(dr["PUBUSER"])) entity.PUBUSER = Convert.ToInt32(dr["PUBUSER"]);
                if (!Convert.IsDBNull(dr["LASTEDITOR"])) entity.LASTEDITOR = Convert.ToString(dr["LASTEDITOR"]);
                if (!Convert.IsDBNull(dr["RCVEDUSERS"])) entity.RCVEDUSERS = Convert.ToString(dr["RCVEDUSERS"]);
                if (!Convert.IsDBNull(dr["PASSED"])) entity.PASSED = Convert.ToInt32(dr["PASSED"]);
                if (!Convert.IsDBNull(dr["UPLOADFILES"])) entity.UPLOADFILES = Convert.ToString(dr["UPLOADFILES"]);
                if (!Convert.IsDBNull(dr["ATUIJIAN"])) entity.ATUIJIAN = Convert.ToInt32(dr["ATUIJIAN"]);                
                list.Add(entity);
            }
            dr.Close();
            return list;
        }
    }
}
